package com.ruoyi.web.controller.system;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ExcelMapUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.AccountWorkorder;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.info.AccountWorkorderInfo;
import com.ruoyi.system.service.IAccountWorkOrderService;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.rmi.ServerException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/workorder")
public class WorkOrderController extends BaseController {
    @Autowired
    IAccountWorkOrderService accountWorkOrderService;
    @GetMapping("/list")
    public TableDataInfo list(AccountWorkorder workorder)
    {
        startPage();
        List<AccountWorkorder> list = accountWorkOrderService.selectWorkOrderList(workorder);
        return getDataTable(list);
    }
    @PostMapping
    public AjaxResult add( @RequestBody AccountWorkorder workorder)
    {
        return toAjax(accountWorkOrderService.insertWorkorder(workorder));
    }

    @GetMapping(value = "/{pid}")
    public AjaxResult getInfo(@PathVariable Long pid)
    {
        return success(accountWorkOrderService.selectWorkorderById(pid));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody AccountWorkorder workorder)
    {
        if(accountWorkOrderService.updateWorkorder(workorder)>0){
            return success();
        }else {
            return error();
        }
    }

    @DeleteMapping("/{pids}")
    public AjaxResult remove(@PathVariable Long[] pids)
    {
        return toAjax(accountWorkOrderService.deleteWorkorderByIds(pids));
    }

    @Resource
    ExcelMapUtils excelMapUtils;

    @PostMapping("/export")
    @SneakyThrows
    public void export(HttpServletResponse response, AccountWorkorder workorder)
    {
        List<AccountWorkorder> list = accountWorkOrderService.selectWorkOrderList(workorder);
        List<AccountWorkorderInfo> infoList = list.stream().map(t -> {
            AccountWorkorderInfo info = new AccountWorkorderInfo();
            BeanUtils.copyProperties(t,info);
            info.setCreateTime(DateUtil.formatDateTime(t.getCreateTime()));
            return info;
        }).collect(Collectors.toList());
        List<String> headerList= Arrays.asList("A1,D1,工单统计",
                "A2,A2,客户姓名","B2,B2,车牌号","C2,C2,处理状态","D2,D2,创建时间");
        excelMapUtils.exportExcelListMultiHeader("工单统计",headerList,2,infoList,AccountWorkorderInfo.class,response);
    }
}
